{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- py的切片是左闭右开\n",
    "- 全部使用索引\n",
    "- 防止越界\n",
    "- return a if true else b ，类似于java中的三目运算符\n",
    "- a //2 结果直接截断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "def search(p,arr,low,high)->int:\n",
    "    m = (high + low)//2\n",
    "    v = arr[m]\n",
    "    if v == p:\n",
    "        return  m\n",
    "    elif p < v:\n",
    "        return search(p,arr,low,m-1) if low < m else -1\n",
    "    elif p > v:\n",
    "        return search(p,arr,m+1,high) if high > m else -1\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def search(p,arr,low,high)->int:\n",
    "    if low > high:\n",
    "        return -1\n",
    "    m = (high + low)//2\n",
    "    v = arr[m]\n",
    "    if v == p:\n",
    "        return  m\n",
    "    elif p < v:\n",
    "        return search(p,arr,low,m-1)\n",
    "    elif p > v:\n",
    "        return search(p,arr,m+1,high)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = [1,2,3,4,5,100,200]\n",
    "i = search(101,arr,0,len(arr)-1)\n",
    "print(i)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
